package com.beyond.pm.component.chart

import android.view.ViewGroup
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.viewinterop.AndroidView
import com.beyond.pm.R
import com.beyond.theme.orange500
import com.github.mikephil.charting.charts.BarChart
import com.github.mikephil.charting.components.Description
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.BarData
import com.github.mikephil.charting.data.BarDataSet
import com.github.mikephil.charting.data.BarEntry

/**
 * @author chen
 * @time 2023/6/12 14:40
 * @name ProjectManage
 * @class describe
 */
@Composable
fun BarChart(
    modifier: Modifier = Modifier,
    entries: List<BarEntry>,
    dataSetLabel: String,
    dataSetColor: Color = MaterialTheme.colorScheme.primary,
    animateX: Int = 500,
    animateY: Int = 500
) {
    var chart by remember { mutableStateOf<BarChart?>(null) }
    val tColor = MaterialTheme.colorScheme.onBackground.toArgb()
    Box(modifier = modifier.fillMaxSize()) {
        Surface(color = MaterialTheme.colorScheme.background) {
            AndroidView(
                factory = { context ->
                    BarChart(context).apply {
                        layoutParams = ViewGroup.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT
                        )
                        xAxis.apply {
                            setDrawAxisLine(true) // X坐标轴的线
                            setDrawGridLines(false) //
                            axisLineWidth = 1f
                            setAvoidFirstLastClipping(true)
                            position = XAxis.XAxisPosition.BOTTOM
                            textColor = tColor
                            axisLineColor = tColor
                        }
                        axisLeft.apply {
                            setDrawAxisLine(true)
                            enableGridDashedLine(10f, 10f,10f)
                            setDrawGridLines(true)
                            axisLineWidth = 1f
                            axisLineColor = tColor
                            gridColor = tColor
                            textColor = tColor
                            spaceTop = 25f
                            spaceBottom = 25f
                            axisMinimum = 0f
                            axisMinimum = 0f // 设置最小值为 0
                            granularity = 1f // 设置间隔为 1
                        }
                        setDrawMarkers(false)
                        axisRight.apply {
                            this.isEnabled = false
                        }
                        legend.apply {
                            textColor = tColor
                            horizontalAlignment = Legend.LegendHorizontalAlignment.CENTER
                        }
                        data = BarData()
                        description = Description().apply {
                            text = ""
                        }
                        setDrawValueAboveBar(true)
                        setNoDataText(context.getString(R.string.no_data))
                        isScaleYEnabled = true
                        setTouchEnabled(true)
                        setScaleEnabled(false)
                        setPinchZoom(true)
                        setDrawGridBackground(false)
                        setDrawBorders(false)
                        animateX(animateX)
                        animateY(animateY)
                    }
                },
                update = { view ->
                    chart = view
                    updateChart(chart, entries, dataSetLabel, dataSetColor)
                }
            )
        }
    }
}

private fun updateChart(
    chart: BarChart?,
    entries: List<BarEntry>,
    dataSetLabel: String,
    dataSetColor: Color
) {
    chart?.let {
        val dataSet = BarDataSet(entries, dataSetLabel)
        dataSet.color = dataSetColor.toArgb()
        dataSet.valueTextColor = orange500.toArgb()
        dataSet.valueTextSize = 10f
        val barData = BarData(dataSet)
        barData.barWidth = 0.4f // 设置数据条的宽度
        it.data = barData
        it.setFitBars(true) // 让X轴与所有的数据条适配
        it.invalidate()
    }
}